<html>

<head>
<style type="text/css" title="currentStyle">
	@import "/apps/resources/css/github.css";
</style>
</head>

<body>

<div class="markdown-body">
      <h1>
<a id="user-content-google-guice-with-jpa-configuration" class="anchor" href="#google-guice-with-jpa-configuration" aria-hidden="true"><span class="octicon octicon-link"></span></a>Google Guice with JPA configuration</h1>

<p>The following sections will get you started with the basic configuration of google-guice with JPA with a complete working example. The source code can be downloaded as well. This is intended for a beginner not for an advanced user.</p>

<hr>

<h3>
<a id="user-content-1-add-dependencies" class="anchor" href="#1-add-dependencies" aria-hidden="true"><span class="octicon octicon-link"></span></a>1. Add dependencies</h3>

<hr>

<p>File : pom.xml</p>

<div class="highlight highlight-xml"><pre>&lt;<span class="pl-ent">project</span> <span class="pl-e">xmlns</span>=<span class="pl-s1"><span class="pl-pds">"</span>http://maven.apache.org/POM/4.0.0<span class="pl-pds">"</span></span> <span class="pl-e">xmlns</span><span class="pl-e">:</span><span class="pl-e">xsi</span>=<span class="pl-s1"><span class="pl-pds">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="pl-pds">"</span></span> <span class="pl-e">xsi</span><span class="pl-e">:</span><span class="pl-e">schemaLocation</span>=<span class="pl-s1"><span class="pl-pds">"</span>http://maven.apache.org/POM/4.0.0 </span>
<span class="pl-s1">    http://maven.apache.org/maven-v4_0_0.xsd<span class="pl-pds">"</span></span>&gt;
    &lt;<span class="pl-ent">modelVersion</span>&gt;4.0.0&lt;/<span class="pl-ent">modelVersion</span>&gt;
    &lt;<span class="pl-ent">groupId</span>&gt;com.prapps.tutorial&lt;/<span class="pl-ent">groupId</span>&gt;
    &lt;<span class="pl-ent">artifactId</span>&gt;google-guice-jpa&lt;/<span class="pl-ent">artifactId</span>&gt;
    &lt;<span class="pl-ent">packaging</span>&gt;jar&lt;/<span class="pl-ent">packaging</span>&gt;
    &lt;<span class="pl-ent">version</span>&gt;1.0.0-SNAPSHOT&lt;/<span class="pl-ent">version</span>&gt;
    &lt;<span class="pl-ent">name</span>&gt;google-guice&lt;/<span class="pl-ent">name</span>&gt;
    &lt;<span class="pl-ent">url</span>&gt;http://maven.apache.org&lt;/<span class="pl-ent">url</span>&gt;

    &lt;<span class="pl-ent">dependencies</span>&gt;
        &lt;<span class="pl-ent">dependency</span>&gt;
            &lt;<span class="pl-ent">groupId</span>&gt;log4j&lt;/<span class="pl-ent">groupId</span>&gt;
            &lt;<span class="pl-ent">artifactId</span>&gt;log4j&lt;/<span class="pl-ent">artifactId</span>&gt;
            &lt;<span class="pl-ent">version</span>&gt;1.2.17&lt;/<span class="pl-ent">version</span>&gt;
        &lt;/<span class="pl-ent">dependency</span>&gt;
        &lt;<span class="pl-ent">dependency</span>&gt;
            &lt;<span class="pl-ent">groupId</span>&gt;junit&lt;/<span class="pl-ent">groupId</span>&gt;
            &lt;<span class="pl-ent">artifactId</span>&gt;junit&lt;/<span class="pl-ent">artifactId</span>&gt;
            &lt;<span class="pl-ent">version</span>&gt;4.11&lt;/<span class="pl-ent">version</span>&gt;
        &lt;/<span class="pl-ent">dependency</span>&gt;
        &lt;<span class="pl-ent">dependency</span>&gt;
            &lt;<span class="pl-ent">groupId</span>&gt;com.google.inject&lt;/<span class="pl-ent">groupId</span>&gt;
            &lt;<span class="pl-ent">artifactId</span>&gt;guice&lt;/<span class="pl-ent">artifactId</span>&gt;
            &lt;<span class="pl-ent">version</span>&gt;3.0&lt;/<span class="pl-ent">version</span>&gt;
        &lt;/<span class="pl-ent">dependency</span>&gt;
        &lt;<span class="pl-ent">dependency</span>&gt;
            &lt;<span class="pl-ent">groupId</span>&gt;org.hibernate.javax.persistence&lt;/<span class="pl-ent">groupId</span>&gt;
            &lt;<span class="pl-ent">artifactId</span>&gt;hibernate-jpa-2.1-api&lt;/<span class="pl-ent">artifactId</span>&gt;
            &lt;<span class="pl-ent">version</span>&gt;1.0.0.Final&lt;/<span class="pl-ent">version</span>&gt;
        &lt;/<span class="pl-ent">dependency</span>&gt;

        &lt;<span class="pl-ent">dependency</span>&gt;
            &lt;<span class="pl-ent">groupId</span>&gt;com.h2database&lt;/<span class="pl-ent">groupId</span>&gt;
            &lt;<span class="pl-ent">artifactId</span>&gt;h2&lt;/<span class="pl-ent">artifactId</span>&gt;
            &lt;<span class="pl-ent">version</span>&gt;1.4.185&lt;/<span class="pl-ent">version</span>&gt;
        &lt;/<span class="pl-ent">dependency</span>&gt;
        &lt;<span class="pl-ent">dependency</span>&gt;
            &lt;<span class="pl-ent">groupId</span>&gt;com.google.inject.extensions&lt;/<span class="pl-ent">groupId</span>&gt;
            &lt;<span class="pl-ent">artifactId</span>&gt;guice-persist&lt;/<span class="pl-ent">artifactId</span>&gt;
            &lt;<span class="pl-ent">version</span>&gt;3.0&lt;/<span class="pl-ent">version</span>&gt;
        &lt;/<span class="pl-ent">dependency</span>&gt;
        &lt;<span class="pl-ent">dependency</span>&gt;
            &lt;<span class="pl-ent">groupId</span>&gt;org.hibernate&lt;/<span class="pl-ent">groupId</span>&gt;
            &lt;<span class="pl-ent">artifactId</span>&gt;hibernate-core&lt;/<span class="pl-ent">artifactId</span>&gt;
            &lt;<span class="pl-ent">version</span>&gt;4.3.8.Final&lt;/<span class="pl-ent">version</span>&gt;
        &lt;/<span class="pl-ent">dependency</span>&gt;
        &lt;<span class="pl-ent">dependency</span>&gt;
            &lt;<span class="pl-ent">groupId</span>&gt;org.hibernate&lt;/<span class="pl-ent">groupId</span>&gt;
            &lt;<span class="pl-ent">artifactId</span>&gt;hibernate-entitymanager&lt;/<span class="pl-ent">artifactId</span>&gt;
            &lt;<span class="pl-ent">version</span>&gt;4.3.8.Final&lt;/<span class="pl-ent">version</span>&gt;
        &lt;/<span class="pl-ent">dependency</span>&gt;

    &lt;/<span class="pl-ent">dependencies</span>&gt;
&lt;/<span class="pl-ent">project</span>&gt;</pre></div>

<h3>
<a id="user-content-2-create-the-project-structure" class="anchor" href="#2-create-the-project-structure" aria-hidden="true"><span class="octicon octicon-link"></span></a>2. Create the project structure</h3>

<p><img src="/apps/resources/images/tutorials/project_structure-google-guice-jpa.png" alt="project structure"></p>

<h3>
<a id="user-content-3-create-the-vo-class" class="anchor" href="#3-create-the-vo-class" aria-hidden="true"><span class="octicon octicon-link"></span></a>3. Create the VO class</h3>

<pre><code>package com.prapps.tutorial.guice.vo;

public class Employee {

    private long id;
    private String name;
    private double salary;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
}
</code></pre>

<h3>
<a id="user-content-4-create-the-entity" class="anchor" href="#4-create-the-entity" aria-hidden="true"><span class="octicon octicon-link"></span></a>4. Create the Entity</h3>

<pre><code>package com.prapps.tutorial.guice.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="EMPLOYEE")
public class Employee {

    @Id
    @Column(name="ID")
    private long id;
    @Column(name="NAME")
    private String name;
    @Column(name="SALARY")
    private double salary;

    public Employee() {
        // TODO Auto-generated constructor stub
    }

    public Employee(long id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
    }

}
</code></pre>

<h3>
<a id="user-content-5-add-the-service-interface" class="anchor" href="#5-add-the-service-interface" aria-hidden="true"><span class="octicon octicon-link"></span></a>5. Add the Service Interface</h3>

<pre><code>package com.prapps.tutorial.guice.service.api;

import java.util.Collection;

import com.prapps.tutorial.guice.vo.Employee;

public interface EmployeeService {

    void addEmployee(Employee employee);

    Collection&lt;Employee&gt; searchEmployeeByName(String name);

    Employee searchEmployeeById(Long id);
}
</code></pre>

<h3>
<a id="user-content-6-add-the-dao-interface" class="anchor" href="#6-add-the-dao-interface" aria-hidden="true"><span class="octicon octicon-link"></span></a>6. Add the DAO Interface</h3>

<pre><code>package com.prapps.tutorial.guice.dao.api;

import java.util.List;

import com.prapps.tutorial.guice.entities.Employee;

public interface EmployeeDao {

    void save(Employee employee);
    Employee findEmployeeById(long id);
    List&lt;Employee&gt; findEmployee(String name);
}
</code></pre>

<h3>
<a id="user-content-7-service-implementation" class="anchor" href="#7-service-implementation" aria-hidden="true"><span class="octicon octicon-link"></span></a>7. Service Implementation</h3>

<pre><code>package com.prapps.tutorial.guice.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import com.prapps.tutorial.guice.dao.api.EmployeeDao;
import com.prapps.tutorial.guice.service.api.EmployeeService;
import com.prapps.tutorial.guice.vo.Employee;

public class EmployeeServiceImpl implements EmployeeService {

    @Inject private EmployeeDao employeeDao;

    @Transactional
    public void addEmployee(Employee employee) {
        com.prapps.tutorial.guice.entities.Employee emp = new com.prapps.tutorial.guice.entities.Employee(
                employee.getId(),
                employee.getName(),
                employee.getSalary()
                );
        employeeDao.save(emp);
    }

    public Collection&lt;Employee&gt; searchEmployeeByName(String name) {
        Collection&lt;Employee&gt; result = new ArrayList&lt;Employee&gt;();
        List&lt;com.prapps.tutorial.guice.entities.Employee&gt; employees = employeeDao.findEmployee(name);
        for(com.prapps.tutorial.guice.entities.Employee emp : employees) {
            result.add(new Employee(emp.getId(), emp.getName(), emp.getSalary()));
        }
        return result;
    }

    public Employee searchEmployeeById(Long id) {
        com.prapps.tutorial.guice.entities.Employee employee = employeeDao.findEmployeeById(id);
        Employee empvo = new Employee();
        empvo.setId(employee.getId());
        empvo.setName(employee.getName());
        empvo.setSalary(employee.getSalary());
        return empvo;
    }

}
</code></pre>

<p>It is important to note that the method <strong>addEmployee</strong> is annotated with <strong>com.google.inject.persist.Transactional</strong>.</p>

<h3>
<a id="user-content-8-dao-implementation" class="anchor" href="#8-dao-implementation" aria-hidden="true"><span class="octicon octicon-link"></span></a>8. DAO Implementation</h3>

<p>Note: When the DAO class is singleton the entityManager Provider should be used instead of directly injecting the entityManager by @inject EntityManager entityManager as in the later approach every invocation will be a new transaction. </p>

<pre><code>package com.prapps.tutorial.guice.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.hibernate.Session;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.prapps.tutorial.guice.dao.api.EmployeeDao;
import com.prapps.tutorial.guice.entities.Employee;

public class EmployeeDaoImpl implements EmployeeDao {

    @Inject private Provider&lt;EntityManager&gt; emp;

    private Session getSession() {
        return emp.get().unwrap(Session.class);
    }

    public Employee findEmployeeById(long id) {
        return emp.get().find(Employee.class, id);
    }

    public List&lt;Employee&gt; findEmployee(String name) {
        /*Criteria criteria = getSession().createCriteria(Employee.class);
        criteria.add(Restrictions.eq("name", name));
        return (List&lt;Employee&gt;) criteria.list();*/
        CriteriaQuery&lt;Employee&gt; createQuery = emp.get().getCriteriaBuilder().createQuery(Employee.class);
        Root&lt;Employee&gt; root = createQuery.from(Employee.class);
        createQuery.where(root.get("name").in(name));
        return emp.get().createQuery(createQuery).getResultList();
    }

    public void save(Employee employee) {
        emp.get().merge(employee);
    }
}
</code></pre>

<h3>
<a id="user-content-9-persistencexml" class="anchor" href="#9-persistencexml" aria-hidden="true"><span class="octicon octicon-link"></span></a>9. persistence.xml</h3>

<p>A simple database configuration with H2 database. Note the persistent-unit name should match while installing the JPAPersistModule in the next section.</p>

<div class="highlight highlight-xml"><pre>&lt;<span class="pl-ent">persistence</span> <span class="pl-e">xmlns</span>=<span class="pl-s1"><span class="pl-pds">"</span>http://java.sun.com/xml/ns/persistence<span class="pl-pds">"</span></span> <span class="pl-e">xmlns</span><span class="pl-e">:</span><span class="pl-e">xsi</span>=<span class="pl-s1"><span class="pl-pds">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="pl-pds">"</span></span>
    <span class="pl-e">xsi</span><span class="pl-e">:</span><span class="pl-e">schemaLocation</span>=<span class="pl-s1"><span class="pl-pds">"</span>http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd<span class="pl-pds">"</span></span> <span class="pl-e">version</span>=<span class="pl-s1"><span class="pl-pds">"</span>2.0<span class="pl-pds">"</span></span>&gt;
    &lt;<span class="pl-ent">persistence-unit</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>employee-persister<span class="pl-pds">"</span></span> <span class="pl-e">transaction-type</span>=<span class="pl-s1"><span class="pl-pds">"</span>RESOURCE_LOCAL<span class="pl-pds">"</span></span>&gt;
        &lt;<span class="pl-ent">provider</span>&gt;org.hibernate.ejb.HibernatePersistence&lt;/<span class="pl-ent">provider</span>&gt;

        &lt;<span class="pl-ent">class</span>&gt;com.prapps.tutorial.guice.entities.Employee&lt;/<span class="pl-ent">class</span>&gt;

        &lt;<span class="pl-ent">properties</span>&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>javax.persistence.jdbc.driver<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>org.h2.Driver<span class="pl-pds">"</span></span> /&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>javax.persistence.jdbc.url<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>jdbc:h2:mem:test<span class="pl-pds">"</span></span> /&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>javax.persistence.jdbc.user<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>sa<span class="pl-pds">"</span></span> /&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>javax.persistence.jdbc.password<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span><span class="pl-pds">"</span></span> /&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>hibernate.dialect<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>org.hibernate.dialect.H2Dialect<span class="pl-pds">"</span></span> /&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>hibernate.hbm2ddl.auto<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>create-drop<span class="pl-pds">"</span></span> /&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>show_sql<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>true<span class="pl-pds">"</span></span> /&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>hibernate.show_sql<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>true<span class="pl-pds">"</span></span> /&gt;
            &lt;<span class="pl-ent">property</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>hibernate.temp.use_jdbc_metadata_defaults<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>false<span class="pl-pds">"</span></span> /&gt;
        &lt;/<span class="pl-ent">properties</span>&gt;
    &lt;/<span class="pl-ent">persistence-unit</span>&gt;
&lt;/<span class="pl-ent">persistence</span>&gt;</pre></div>

<h3>
<a id="user-content-10-start-the-persistence-service" class="anchor" href="#10-start-the-persistence-service" aria-hidden="true"><span class="octicon octicon-link"></span></a>10. Start the Persistence Service</h3>

<p>This class starts the persistence service otherwise it will throw a Null
PointerException while accessing database. This can be further configured based on needs.</p>

<pre><code>package com.prapps.tutorial.guice.ioc;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.persist.PersistService;

@Singleton
public class PersistenceInitializer {

    @Inject
    PersistenceInitializer(PersistService service) {
        service.start();
        // At this point JPA is started and ready.
    }
}
</code></pre>

<h3>
<a id="user-content-11-guice-configuration" class="anchor" href="#11-guice-configuration" aria-hidden="true"><span class="octicon octicon-link"></span></a>11. Guice configuration</h3>

<pre><code>package com.prapps.tutorial.guice.ioc;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.matcher.Matchers;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.prapps.tutorial.guice.aop.LogAspect;
import com.prapps.tutorial.guice.dao.api.EmployeeDao;
import com.prapps.tutorial.guice.dao.impl.EmployeeDaoImpl;
import com.prapps.tutorial.guice.service.api.EmployeeService;
import com.prapps.tutorial.guice.service.impl.EmployeeServiceImpl;

public class EmployeeAppInjector extends AbstractModule {

    @Override
    protected void configure() {
        // "employee-persister" is defined in the persistence.xml
        install(new JpaPersistModule("employee-persister"));
        bind(EmployeeService.class).to(EmployeeServiceImpl.class).in(Singleton.class);
        bind(EmployeeDao.class).to(EmployeeDaoImpl.class).in(Singleton.class);
        LogAspect logAspect = new LogAspect();
        requestInjection(logAspect);
        bindInterceptor(Matchers.subclassesOf(EmployeeService.class), Matchers.any(), logAspect);
        bind(PersistenceInitializer.class);
    }
}
</code></pre>

<p>For deploying in web environments 
      <code>filter("/*").through(PersistFilter.class);</code>
should be used and PersistenceInitializer is not required.</p>

<h3>
<a id="user-content-12-test-client---add-the-client-to-test-the-configuration" class="anchor" href="#12-test-client---add-the-client-to-test-the-configuration" aria-hidden="true"><span class="octicon octicon-link"></span></a>12. Test Client - Add the client to test the configuration</h3>

<pre><code>package com.prapps.tutorial.guice;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.prapps.tutorial.guice.ioc.EmployeeAppInjector;
import com.prapps.tutorial.guice.ioc.PersistenceInitializer;
import com.prapps.tutorial.guice.service.api.EmployeeService;
import com.prapps.tutorial.guice.vo.Employee;

public class GuiceTestClient {

    private static final Logger LOG = Logger.getLogger(GuiceTestClient.class);
    private EmployeeService employeeService; 

    @Before public void setUp() {
        Injector injector = Guice.createInjector(new EmployeeAppInjector());
        //To start the jpa service add the following line        
        PersistenceInitializer ps = injector.getInstance(PersistenceInitializer.class);
        employeeService = injector.getInstance(EmployeeService.class);
    }

    @Test
    public void testAddEmployee() {
        Employee employee = new Employee();
        employee.setId(101l);
        employee.setName("Souvik Majumder");
        employee.setSalary(1234.0d);
        employeeService.addEmployee(employee);

        employee = new Employee();
        employee.setId(102l);
        employee.setName("Pratik Sengupta");
        employee.setSalary(4321.0d);
        employeeService.addEmployee(employee);

        employee = new Employee();
        employee.setId(103l);
        employee.setName("Debojit Chatterjee");
        employee.setSalary(8675.0d);
        employeeService.addEmployee(employee);
    }

    @Test
    public void testSearchEmployee() {
        testAddEmployee();

        Employee employee = employeeService.searchEmployeeById(101l);
        assert("Souvik Majumder".equals(employee.getName()));
        assert(1234d == employee.getSalary());
    }

    @Test
    public void testSearchEmployeeByName() {
        testAddEmployee();

        Collection&lt;Employee&gt; employees = employeeService.searchEmployeeByName("Pratik Sengupta");
        assert(1 == employees.size());

        Employee employee = employees.iterator().next();
        assert("Pratik Sengupta".equals(employee.getName()));
        assert(4321d == employee.getSalary());
        LOG.debug(employee);
    }
}
</code></pre>

<h3>
<a id="user-content-13-log4jxml" class="anchor" href="#13-log4jxml" aria-hidden="true"><span class="octicon octicon-link"></span></a>13. Log4j.xml</h3>

<div class="highlight highlight-xml"><pre>&lt;?<span class="pl-ent">xml</span><span class="pl-e"> version</span>=<span class="pl-s1"><span class="pl-pds">"</span>1.0<span class="pl-pds">"</span></span><span class="pl-e"> encoding</span>=<span class="pl-s1"><span class="pl-pds">"</span>UTF-8<span class="pl-pds">"</span></span> ?&gt;
&lt;!<span class="pl-k">DOCTYPE</span> <span class="pl-v">log4j:configuration</span> SYSTEM "log4j.dtd"&gt;
&lt;<span class="pl-ent">log4j</span><span class="pl-ent">:</span><span class="pl-ent">configuration</span> <span class="pl-e">debug</span>=<span class="pl-s1"><span class="pl-pds">"</span>true<span class="pl-pds">"</span></span>
                     <span class="pl-e">xmlns</span><span class="pl-e">:</span><span class="pl-e">log4j</span>=<span class="pl-s1"><span class="pl-pds">'</span>http://jakarta.apache.org/log4j/<span class="pl-pds">'</span></span>&gt;

   &lt;<span class="pl-ent">appender</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>consoleAppender<span class="pl-pds">"</span></span> <span class="pl-e">class</span>=<span class="pl-s1"><span class="pl-pds">"</span>org.apache.log4j.ConsoleAppender<span class="pl-pds">"</span></span>&gt;
      &lt;<span class="pl-ent">layout</span> <span class="pl-e">class</span>=<span class="pl-s1"><span class="pl-pds">"</span>org.apache.log4j.PatternLayout<span class="pl-pds">"</span></span>&gt;
         &lt;<span class="pl-ent">param</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>ConversionPattern<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n<span class="pl-pds">"</span></span>/&gt;
      &lt;/<span class="pl-ent">layout</span>&gt;
   &lt;/<span class="pl-ent">appender</span>&gt;

   &lt;<span class="pl-ent">appender</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>fileAppender<span class="pl-pds">"</span></span> <span class="pl-e">class</span>=<span class="pl-s1"><span class="pl-pds">"</span>org.apache.log4j.RollingFileAppender<span class="pl-pds">"</span></span>&gt;
      &lt;<span class="pl-ent">param</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>append<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>false<span class="pl-pds">"</span></span>/&gt;
      &lt;<span class="pl-ent">param</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>file<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>out/learning.log<span class="pl-pds">"</span></span>/&gt;
      &lt;<span class="pl-ent">layout</span> <span class="pl-e">class</span>=<span class="pl-s1"><span class="pl-pds">"</span>org.apache.log4j.PatternLayout<span class="pl-pds">"</span></span>&gt;
         &lt;<span class="pl-ent">param</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>ConversionPattern<span class="pl-pds">"</span></span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>%d{ABSOLUTE} %-5p [%c{1}] %m%n<span class="pl-pds">"</span></span>/&gt;
      &lt;/<span class="pl-ent">layout</span>&gt;
   &lt;/<span class="pl-ent">appender</span>&gt;

    &lt;<span class="pl-ent">logger</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>org.hibernate<span class="pl-pds">"</span></span> <span class="pl-e">additivity</span>=<span class="pl-s1"><span class="pl-pds">"</span>false<span class="pl-pds">"</span></span>&gt;  
        &lt;<span class="pl-ent">level</span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>trace<span class="pl-pds">"</span></span> /&gt;
        &lt;<span class="pl-ent">appender-ref</span> <span class="pl-e">ref</span>=<span class="pl-s1"><span class="pl-pds">"</span>consoleAppender<span class="pl-pds">"</span></span> /&gt;
    &lt;/<span class="pl-ent">logger</span>&gt; 

    &lt;<span class="pl-ent">logger</span> <span class="pl-e">name</span>=<span class="pl-s1"><span class="pl-pds">"</span>com.prapps.tutorial<span class="pl-pds">"</span></span> <span class="pl-e">additivity</span>=<span class="pl-s1"><span class="pl-pds">"</span>false<span class="pl-pds">"</span></span>&gt;  
        &lt;<span class="pl-ent">level</span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>trace<span class="pl-pds">"</span></span> /&gt;
        &lt;<span class="pl-ent">appender-ref</span> <span class="pl-e">ref</span>=<span class="pl-s1"><span class="pl-pds">"</span>consoleAppender<span class="pl-pds">"</span></span> /&gt;
    &lt;/<span class="pl-ent">logger</span>&gt;

   &lt;<span class="pl-ent">root</span>&gt;
      &lt;<span class="pl-ent">level</span> <span class="pl-e">value</span>=<span class="pl-s1"><span class="pl-pds">"</span>INFO<span class="pl-pds">"</span></span>/&gt;
      &lt;<span class="pl-ent">appender-ref</span> <span class="pl-e">ref</span>=<span class="pl-s1"><span class="pl-pds">"</span>consoleAppender<span class="pl-pds">"</span></span>/&gt;
      &lt;<span class="pl-ent">appender-ref</span> <span class="pl-e">ref</span>=<span class="pl-s1"><span class="pl-pds">"</span>fileAppender<span class="pl-pds">"</span></span>/&gt;
   &lt;/<span class="pl-ent">root</span>&gt;

&lt;/<span class="pl-ent">log4j</span><span class="pl-ent">:</span><span class="pl-ent">configuration</span>&gt;</pre></div>

<h3>
<a id="user-content-conclusion" class="anchor" href="#conclusion" aria-hidden="true"><span class="octicon octicon-link"></span></a>Conclusion</h3>

<p>Point to keep in mind is to start the PersistenceService when using in local environment. For further info please visit <a href="https://github.com/google/guice/wiki/JPA">https://github.com/google/guice/wiki/JPA</a> and to learn about transactional strategies refer to
<a href="https://github.com/google/guice/wiki/Transactions">https://github.com/google/guice/wiki/Transactions</a></p>

    </div>
    
    
    </body>
    </html>